package com.example.ai.agent.mcp.tool;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.stereotype.Component;

import java.util.function.Function;

/**
 * 简单的数学计算工具
 *
 * @author AI Agent
 */
@Component
@Slf4j
public class MathCalculator {

    /**
     * 执行数学计算
     *
     * @param request 计算请求对象
     * @return 计算结果
     * @throws IllegalArgumentException 不支持的操作符或除数为零
     */
    public String calculate(CalculateRequest request) {
        try {
            int x = request.getX();
            int y = request.getY();
            String operator = request.getOperator();
            log.info("calculate：{} {} {} = {}", x, operator, y, x + operator + y);

            double result;
            switch (operator) {
                case "+":
                    result = x + y;
                    break;
                case "-":
                    result = x - y;
                    break;
                case "*":
                    result = x * y;
                    break;
                case "/":
                    if (y == 0) {
                        throw new IllegalArgumentException("除数不能为零");
                    }
                    result = (double) x / y;
                    break;
                default:
                    throw new IllegalArgumentException("不支持的操作符: " + operator + "，支持的操作符: +, -, *, /");
            }
            return String.format("计算结果: %d %s %d = %.2f", x, operator, y, result);
        } catch (IllegalArgumentException e) {
            return "计算错误: " + e.getMessage();
        }
    }

    /**
     * 计算请求类
     */
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class CalculateRequest {
        @JsonProperty("x")
        private int x;

        @JsonProperty("y")
        private int y;

        @JsonProperty("operator")
        private String operator;

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        public String getOperator() {
            return operator;
        }

        public void setOperator(String operator) {
            this.operator = operator;
        }
    }
}